﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM
using YunSpace.Core;
using YunSpace.Domain.Entity.SiteLibraryManage;
using YunSpace.Domain.IRepository.SiteLibraryManage;
using YunSpace.Repository.SiteLibraryManage;
using System.Collections.Generic;
using System.Linq;
using YunSpace.Domain.ViewModel.SiteLibraryManage;
using YunSpace.Data;
#endregion

namespace YunSpace.Application.SiteLibraryManage
{
    public class ServicesCostApp
    {
        private ICostRecordRepository service = new ServicesCostRepository();

        public List<ServicesCostEntity> GetList(Pagination pagination, string queryJson)
        {
            var expression = ExtLinq.True<ServicesCostEntity>();
            var queryParam = queryJson.ToJObject();
            if (!queryParam["F_SpaceId"].IsEmpty())
            {
                string F_SpaceId = queryParam["F_SpaceId"].ToString();
                expression = expression.And(t => t.F_SpaceId == F_SpaceId);
            }
            // if (!queryParam["timeType"].IsEmpty())
            // {
            //     string timeType = queryParam["timeType"].ToString();
            //     DateTime startTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate();
            //     DateTime endTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate().AddDays(1);
            //     switch (timeType)
            //     {
            //         case "1":
            //             break;
            //         case "2":
            //             startTime = DateTime.Now.AddDays(-7);
            //             break;
            //         case "3":
            //             startTime = DateTime.Now.AddMonths(-1);
            //             break;
            //         case "4":
            //             startTime = DateTime.Now.AddMonths(-3);
            //             break;
            //         default:
            //             break;
            //     }
            //     expression = expression.And(t => t.F_Date >= startTime && t.F_Date <= endTime);
            // }
            return service.FindList(expression, pagination);
        }

        public ServicesOutput GetForm(string keyValue)
        {
            using (var db = new RepositoryBase())
            {
                ServicesOutput data = new ServicesOutput();
                string strSql1 = @"select * from Site_ServicesCost where F_Id='" + keyValue + "';";
                string strSql2 = @"select * from SL_Photos where photoable_id='" + keyValue + "' and photoable_type='SiteServer';";

                var data1 = db.FindList<ServicesCostEntity>(strSql1);
                var data2 = db.FindList<PhotosEntity>(strSql2);
                data.servicesCostEntity = data1.FirstOrDefault();
                data.photosEntity = data2.FirstOrDefault();

                return data;
            }
        }
        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public string SubmitForm(ServicesCostEntity servicescostEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                servicescostEntity.Modify(keyValue);
                service.Update(servicescostEntity);
            }
            else
            {
                servicescostEntity.Create();
                // costrecordEntity.F_Category = 2;
                service.Insert(servicescostEntity);
            }

            return servicescostEntity.F_Id;
        }
    }
}

